package com.wolfpire.notice.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.notice.dao.NoticeDao;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.common.util.MatchType;
import com.wolfpire.system.model.Notice;
import com.wolfpire.system.model.Profession;
@Repository("noticeDao")
public class NoticeDaoImpl extends BaseHibernateDao<Notice, Long> 
	implements NoticeDao {

	public Notice getByTitle(String title) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Notice> list(Notice notice) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<Notice> findNotices(Page<Notice> page, Notice filterNotice) {
		Criterion[] criterions = this.createFilter(filterNotice);
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		if (0 < totalCount) {
			Criteria c = createCriteria(criterions);
			//c.addOrder(Order.asc("seq"));
			//c.addOrder(Order.asc("createTime"));                //由小到达
			c.addOrder(Order.desc("createTime"));                 //由大到小            
			c = setPageParameter(c, page);
			@SuppressWarnings("unchecked")
			List<Notice> notices = c.list();
			page.setDataList(notices);
		}
		return page;
	}
	
	private Criterion[] createFilter(Notice notice) {
		if (null == notice) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != notice.getId()) {
			criterions.add(Restrictions.eq("id", notice.getId()));
		}
		if (!StringUtils.isBlank(notice.getTitle())) {
			criterions.add(Restrictions.like("title", notice.getTitle(), MatchMode.ANYWHERE));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

	public List<Notice> list(int num) {
		StringBuffer querySql = new StringBuffer();
		querySql.append(" select * from t_notice n ORDER BY create_time limit ");
		querySql.append(num);
		SQLQuery query = this.createSqlQuery(querySql.toString(), null, null);
		query.addEntity(Notice.class);
		return query.list();
	}

	public Notice findFrontNotice(long id, String type) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select * from t_notice n where n.id = (select max(id) from t_notice where id < :id ");
		propertyNames.add("id");
		propertyValues.add(id);
		querySql.append(" and type = :type)");
		propertyNames.add("type");
		propertyValues.add(type);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(Notice.class);
		return (Notice) query.uniqueResult();
	}

	public Notice findNextNoitce(long id, String type) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select * from t_notice n where n.id = (select min(id) from t_notice where id > :id ");
		propertyNames.add("id");
		propertyValues.add(id);
		querySql.append(" and type = :type)");
		propertyNames.add("type");
		propertyValues.add(type);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(Notice.class);
		return (Notice) query.uniqueResult();
	}

	public Notice currentNotice(long id, String type) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select * from t_notice n where n.id = :id and n.type = :type");
		propertyNames.add("id");
		propertyValues.add(id);
		propertyNames.add("type");
		propertyValues.add(type);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(Notice.class);
		return (Notice) query.uniqueResult();
	}

	
}
